package com.itheima.homework.service.impl;

import com.itheima.homework.dao.StudentDao;
import com.itheima.homework.pojo.Student;
import com.itheima.homework.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public boolean existUserByUsername(String username) {
        return Objects.nonNull(studentDao.selectOneByUsername(username));
    }

    @Override
    public Student register(Student student) {
        studentDao.insertOne(student);
        return student;
    }

    @Override
    public Student getOneByUsername(String username) {
        return studentDao.selectOneByUsername(username);
    }

    @Override
    public void removeByUsername(String username) {
        ArrayList<String> usernameList = new ArrayList<>();
        usernameList.add(username);
        studentDao.deleteBatch(usernameList);
    }

    @Override
    public void updatePassword(Student student) {
        Student oneByUsername = studentDao.selectOneByUsername(student.getUsername());
        oneByUsername.setPassword(student.getNewPassword());
        studentDao.updateByUsername(oneByUsername);
    }

    @Override
    public List<Student> list() {
        return studentDao.selectList();
    }

    @Override
    public void updateByUsername(Student student) {
        studentDao.updateByUsername(student);
    }

    @Override
    public void removeBatch(List<String> usernameList) {
        studentDao.deleteBatch(usernameList);
    }
}
